﻿using Bouyei.GeoCore.Geometries;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bouyei.GeoCore.GeoParsers.GeoJson
{
    public class GeoJsonMultiPoint<T, MPoint>
          : GeoJsonObject<T, MPoint> where MPoint : PointMArray
    {
        public GeoJsonMultiPoint() { }
        public GeoJsonMultiPoint(Feature<T, MPoint>[] features)
         : base()
        {
            this.features = features;
        }
    }

    public class GeoJsonMultiPoint : GeoJsonGeometry
    {
        public List<double[]> coordinates { get; set; }

        public GeoJsonMultiPoint() { }

        public GeoJsonMultiPoint(string jsonString)
        {
            var geo = jsonConvert.ConvertTo<GeoJsonMultiPoint>(jsonString);
            this.coordinates = geo.coordinates;
            this.type = geo.type;
        }

        public GeoJsonMultiPoint(Geometry geometry)
        {
            this.Geometry = geometry;
        }

        public Geometry ToGeometry()
        {
            var coords = new Coordinate[coordinates.Count];
            for (int i = 0; i < coordinates.Count; ++i)
            {
                coords[i] = new Coordinate()
                {
                    X = coordinates[i][0],
                    Y = coordinates[i][1],
                    Z = coordinates[i].Length >= 3 ? coordinates[i][2] : 0
                };
            }
            var gtype = GeometryType;
            var geo = new Geometry(gtype, coords);
            return geo;
        }

        public string FromGeometry()
        {
            this.type = getType();
            var seqs = Geometry.GetGemoetry(0)[0];
            coordinates = new List<double[]>(seqs.Count);

            for (int i = 0; i < seqs.Count; ++i)
            {
                coordinates.Add(new double[] {
                 seqs[i].X,
                 seqs[i].Y
                });
            }

            return jsonConvert.ConvertFrom(this);
        }
    }

}
